package com.csu.servlet;

import com.csu.entiry.Book;
import com.csu.entiry.BorrowInfo;
import com.csu.service.BorrowInfoService;
import com.csu.service.impl.BorrowInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet(name = "GetBorrowInfoServlet", urlPatterns = "/GetBorrowInfoServlet")
public class GetBorrowInfoServlet extends HttpServlet {
    private BorrowInfoService borrowInfoService = new BorrowInfoServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String current = req.getParameter("borrowInfocurrent");
        String size = req.getParameter("size");
        int c, s;
        if (current == null || "".equals(current)) {
            c = 1;
        } else {
            c = Integer.parseInt(current);
        }
        if (size == null || "".equals(size)) {
            s = 10;
        } else {
            s = Integer.parseInt(size);
        }
        String key = req.getParameter("key");
        List<BorrowInfo> borrowInfos = borrowInfoService.getBorrowInfoList(c, s, key);
        int total = borrowInfoService.getTotal(c, s, key);
        int page = (total - 1) / 10 + 1;
        req.getSession().setAttribute("borrowInfoList", borrowInfos);
        req.getSession().setAttribute("borrowInfopage", page);
        req.getSession().setAttribute("borrowInfocurrent", c);
        resp.sendRedirect(req.getContextPath() + "/borrowInfo.jsp");
    }
}
